<template>
  <div class="hawkContent">
    <!-- 面包屑 -->
    <hawk-breadcrumb></hawk-breadcrumb>
    <!-- 搜索、添加区域 ，elememt 24栅格划分区域-->
    <el-row :gutter="20" style="padding-left: 20px">
      <el-col :span="3">
        <el-input placeholder="请输入路由器名称搜索" v-model="queryForm.tname"></el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="hawkSearch" @click="searchData">搜索</el-button>
        <el-button plain @click="reSet">重置</el-button>
      </el-col>
      <el-col
        :span="4"
        style="float: right; text-align: right; padding-right: 30px"
      >
        <el-button type="hawkCreate" @click='editRouter'>创建路由器</el-button>
        <el-button plain>导入</el-button>
      </el-col>
    </el-row>
    <!-- 引入表格组件 -->
    <hawk-table
      :table-data="tableData"
      @selectionChange="handleSelectionChange"
      :options="[
        { key: 'tname', label: '名称' },
        { key: 'tmodel', label: '类型' },
        { key: 'handle',label:'操作'},
      ]"
    >
      <!-- 如数据库内容和展示内容不一致，可在此进行更改 -->
      <template #name="{ row }">
        <span>{{ row.name }}</span>
      </template>
      <template #handle="{ row }">
        <span class="hawk-text-btn" @click="editRouter(row)">编辑</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="delRouter(row)">删除</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="editRouter(row, 1)">查看</span>
      </template>
    </hawk-table>
    <!-- 表格底部 -->
    <div class="bottom-btns">
      <div class="bottom-btns-left">
        <div class="bottom-btns-left-num">
          {{ multipleSelection.length }} 选择
        </div>
        <el-button type="hawkDelete">批量删除</el-button>
        <el-button plain>导出</el-button>
      </div>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout=" prev, pager, next,sizes"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加和修改模态框 -->
    <el-dialog
      :title="formTitle"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed"
    >
      <!-- 内容主题区 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="名称" prop="tname">
          <el-input
            :readonly="formTitle == '查看路由器'"
            v-model="addForm.tname"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="tmode" >
          <el-input v-model="addForm.tmodel"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*
* @author 韩梦远
* @description 资产路由表的增删改查
* @api 192.168.0.68:8082/cmdb-router/
* @since 2021-10-10
* @update 2021-10-12
* @modifier 韩梦远
* @bug 无
* @version v1.0.0
*/
import { getList, doDelete, doAdd, doEdit,searchList } from '@/api/assets/list/router'
import HawkBreadcrumb from '@/components/breadcrumb/Breadcrumb.vue'
import HawkTable from '@/components/table'
export default {
  // import引入的组件需要注入到对象中才能使用
  components: { HawkBreadcrumb, HawkTable },
  data() {
    return {
      formTitle: '创建路由器',
      addDialogVisible: false,
      addForm: {
        tname: '',
        tmodel:''
      },
      addFormRules: {
        tname: [
          { required: true, message: '请输入路由器名称', trigger: 'blur' },
        ],
        tmodel: [
          { required: true, message: '请输入路由器类型', trigger: 'blur' },
        ],
      },
      tableData: [],
      multipleSelection: [],
      total: 0,
      queryForm: {
        tname:''
      },
      page: {
        current: 1,
        size: 10,
      },
    }
  },
  //监听属性 类似于data概念",
  computed: {
    cellStyle() {
      return {
        padding: '8px 0',
      }
    },
  },
  //监控data中的数据变化",
  watch: {},
  methods: {
    async searchData(){
      const res = await searchList(this.queryForm.tname);
      this.tableData = res
      this.total = 1
    },
    //搜索时的重置方法
    reSet() {
      this.queryForm = this.$options.data().queryForm
      this.init()
    },
    async init() {
      const res = await getList(this.page)
      console.log(res)
      this.tableData = res.list
      this.total = res.count
    },
    //执行添加和编辑
    save() {
        this.$refs.addFormRef.validate(async (valid) =>{
            console.log(valid)
            if (!valid) return
            if (this.addForm.id){
                await doEdit(this.addForm)
            }else {
                await doAdd(this.addForm)
            }
            this.addDialogClosed()
        })
    },
    //添加和编辑方法
    editRouter(row,type){
        if (! row.id) {
            this.formTitle = '创建路由器'
        }else {
            if(type){
                this.formTitle = '查看路由器'
                this.addForm = Object.assign({},row)
            }else {
                this.formTitle = '编辑路由器'
                this.addForm = Object.assign({},row)
            }
        }
        this.addDialogVisible = true
    },
    //模态框状态
    addDialogClosed() {
      this.addForm = this.$options.data().addForm
      this.init()
      this.addDialogVisible = false
      this.$refs.addFormRef.resetFields()
    },
    //删除
    delRouter(row){
        this.$confirm('此操作将删除该条数据，是否继续？','提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        }).then(() => {
            doDelete(row.id)
            this.$message({
            type: 'success',
            message: '删除成功!'
        })
        if(this.tableData.length == 1){
          this.page.current = this.page.current -1
          if(this.page.current < 1){
            this.page.current = 1
          }
        }
        this.init()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
        this.page.size = val;
        this.init()
    },
    handleCurrentChange(val) {
        this.page.current = val;
        this.init()
    },
  },
  //生命周期 - 创建之前",数据模型未加载,方法未加载,html模板未加载
  beforeCreate() {},

  //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created() {
    this.init()
  },

  //生命周期 - 挂载完成（可以访问DOM元素）",html模板已渲染
  mounted() {},
}
</script>

<style lang='scss'>
.hawkContent {
  width: calc(100% - 40px);
  min-width: calc(100vh - 140px);
  background: #fff;
  border-radius: 8px;

  .bottom-btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    margin-top: 10px;

    &-left {
      display: flex;
      align-items: center;

      &-num {
        margin-right: 10px;
        color: #333333;
        font-size: 14px;
      }
    }
  }
}
</style>